
layui.use(['table','jquery','form'],function(){
    var table = layui.table;
    var $=layui.jquery;
    var form=layui.form;

    table.render({
      elem: '#meun',
      url:'./getmeun',
      toolbar: '#toolbarDemo',
      method:"post",
      title: '用户数据表',
      cols: [[
          {type: 'checkbox', fixed: 'left'},
          {field:'m_id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true},
          {field:'m_name', title:'菜单名称', width:120, edit: 'text'},
          {field:'m_url', title:'菜单路径', width:150, edit: 'text'},
          {field:'m_no', title:'菜单编号', width:80, edit: 'text', sort: true},
          {field:'m_status', title:'菜单状态', width:100,templet:'#switchTpl',unresize:true},
          {field:'m_parent', title:'父级菜单'},
          {field:'m_icon', title:'菜单图标', width:80, sort: true,edit: 'text'},
          {field:'m_orderby', title:'菜单序号', width:120,edit: 'text'},
          {fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
      ]],
      page: true,
      limits:[10,20,30,40,50]
    });
  
    //头工具栏事件
    table.on('toolbar(meun)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
        var data = checkStatus.data;
        layer.alert(JSON.stringify(data));
        break;
        case 'getCheckLength':
        var data = checkStatus.data;
        layer.msg('选中了：'+ data.length + ' 个');
        break;
        case 'isAll':
        layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
      };
    });

    //监听行工具事件
    table.on('tool(meun)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
          //发送ajax
          $.ajax({
            url:"./deleteByMeunId",
            data:{mId:obj.data.m_id},
            type:"post",
            dataType:"json"
          }).then(function(res){
              if(res.status==1){
                  obj.del();
                  layer.close(index);
              }
          })      
        });
      }
    });

     //监听单元格编辑
    table.on('edit(meun)', function(obj){
        var value = obj.value; //得到修改后的值
        //data = obj.data, //得到所在行所有键值
        // console.log(data);
        //field = obj.field; //得到字段
        $.ajax({
           url:"http://127.0.0.1:8080/admin/updateMeun",
           data:obj.data,
           method:"post",
           dataType:"json"
        }).then(function(res){
            if(res.status==1){
              //layer.msg("修改成功")
                layer.alert(res.msg,{
                  skin:'layui-layer-molv',//样式类名
                  closeBtn:0
                })
            }else{
             // layer.msg('修改失败34567')
                layer.alert(res.msg,{
                  skin:'layui-layer-lan',//样式类名
                  closeBtn:0,
                  anim:4 //动画类型
                })
            }
        })
    });

    //监听性别操作
    form.on('switch(m_status)',function(obj){
       // console.log(obj.elem.checked)
       let status=obj.elem.checked?1:0;
       console.log(status);
    });
});



